import Vue from 'vue'
import Router from 'vue-router'
import store from '../vuex/store'//import store 状态管理

Vue.use(Router);
const routes = [
  {
    path: '/',
    name: 'home',
    component: function (resolve) {
      require(['../views/index/Index'], resolve)
    }
  },
  {//装修信息
    path: '/zxxx/zxxxinfolist',
    component: function (resolve) {
      require(['../views/zxxx/zxxxinfolist'], resolve)
    }
  },
  {//装修信息
    path: '/zxxx/zxxxinfolistnew',
    component: function (resolve) {
      require(['../views/zxxx/zxxxinfolistnew'], resolve)
    }
  },
  {//装修信息
    path: '/zxxx/zxxxinfolisttest',
    component: function (resolve) {
      require(['../views/zxxx/zxxxinfolisttest'], resolve)
    }
  },
  {//装修信息-新增
    path: '/zxxx/InfoCreate',
    component: function (resolve) {
      require(['../views/zxxx/InfoCreate'], resolve)
    }
  },
  {//装修信息-修改
    path: '/zxxx/InfoDetail/:guid',
    component: function (resolve) {
      require(['../views/zxxx/InfoDetail'], resolve)
    }
  },
  {//装修信息-修改
    path: '/zxxx/InfoDetailnew/:guid',
    component: function (resolve) {
      require(['../views/zxxx/InfoDetailnew'], resolve)
    }
  },
  {//装修信息-查询
    path: '/aipa',
    component: function (resolve) {
      require(['../views/public/aipa'], resolve)
    }
  }
];
const router = new Router({
  routes
});
//导航钩子 加载时显示 loading
router.beforeEach(function (to, from, next) {
  var name = to.name;
  if (name == 'wechatMenu') {
    var openId = to.params.openId;
    console.info("*** openId ****:" + openId);
    if (openId) {
      localStorage.setItem('openId', openId);//不能往user里面丢，会有用户状态
    }
  }

  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (store.state.loginState) {  // 通过vuex state获取当前的token是否存在
      if (to.meta.isConfirm) {
        let memberStatus = JSON.parse(localStorage.getItem('user')).memberStatus;
        //console.log(JSON.parse(localStorage.getItem('user')));
        if (memberStatus == null) {
          next({
            path: '/user',//实名认证路由地址
            query: {returnUrl: to.fullPath}
          })
        } else {
          if (memberStatus.identityStatus != '1') {
            next({
              path: '/user',//实名认证路由地址
              query: {returnUrl: to.fullPath}
            })
          }
          else {
            next()
          }
        }
      }
      next();
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }

  else {
    next();
  }

  store.commit('UPDATE_LOADING', {isLoading: true});
  next()
});
router.afterEach(function () {
  //延迟关闭loading，防止一闪而过
  setTimeout(function () {
    store.commit('UPDATE_LOADING', {isLoading: false})
  }, 500)
});


export default router;
